package com.xingkeduo.dto.transter.giftbagdetail;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xingkeduo.utils.DateUtils;
import com.xingkeduo.utils.WeekUtil;

import lombok.Data;

@Data
public class GiftBagDetail {
	private String use_store;
	private String use_date;
	private String start_time;
	private String end_time;
	private String name;
	private String end_date;

	public void covert(JSONObject json) {
		this.use_store = json.getOrDefault("storeNames", "")==null?"":json.getOrDefault("storeNames", "").toString();
		JSONObject marketing = json.getJSONObject("marketingInfo");
		String works = marketing.getOrDefault("weekHourIds", "")==null?"":marketing.getOrDefault("weekHourIds", "").toString();
		if (StringUtils.isBlank(works)||"0".equals(works)) {
			this.use_date = "周一至周日 0-24点";
		} else {
			JSONArray array = marketing.getJSONArray("activityDays");
			if (array != null && array.size() > 0) {
				StringBuffer daysInfo = new StringBuffer();
				int size = array.size();
				for(int i=0;i<size;i++) {
					JSONObject o = array.getJSONObject(i);
					String daysIds = o.getOrDefault("dayids", "")==null?"":o.getOrDefault("dayids", "").toString();
					if (StringUtils.isBlank(daysIds)) {
						continue;
					}
					String startTime = o.getOrDefault("startTime", "").toString();
					int start = Integer.parseInt(DateUtils.getHour(DateUtils.parseDate(startTime)));
					String endTime = o.getOrDefault("endTime", "").toString();
					int end = Integer.parseInt(DateUtils.getHour(DateUtils.parseDate(endTime)));
					if (daysIds.startsWith(",")) {
						daysIds = daysIds.substring(1, daysIds.length());
					}
					if (daysIds.endsWith(",")) {
						daysIds = daysIds.substring(0, daysIds.length() - 1);
					}
					daysInfo.append(WeekUtil.convertWeek(daysIds)).append(" ")
							.append(start).append("-")
							.append(end).append("点; ");
				}
				if(StringUtils.isBlank(daysInfo.toString())) {
					this.use_date ="周一至周日 0-24点";
				}else {
					this.use_date =daysInfo.toString();
				}
			}else {
				this.use_date ="周一至周日 0-24点";
			}
		}
		Date startDate = json.getDate("useStartTime");
		if (startDate != null) {
			this.start_time = DateUtils.formatDate(startDate, DateUtils.DATE_FORMAT_V3);
		}
		Date endDate = json.getDate("useEndTime");
		if (endDate != null) {
			this.end_time = DateUtils.formatDate(endDate, DateUtils.DATE_FORMAT_V3);
		}
		this.name = json.getOrDefault("marketingName", "")==null?"":json.getOrDefault("marketingName", "").toString();
		this.end_date = start_time + "-" + end_time;
	}
}
